package com.xhtt.modules.app.controller.inspection;

import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.modules.inspection.dto.InsTrackDTO;
import com.xhtt.modules.inspection.entity.InsPoint;
import com.xhtt.modules.inspection.service.IInsLinePointService;
import com.xhtt.modules.inspection.service.IInsLineService;
import com.xhtt.modules.inspection.service.IInsPointLabelService;
import com.xhtt.modules.inspection.vo.InsLineVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡检线路 前端控制器
 * </p>
 *
 * @author xj
 * @since 2019-01-18
 */
@RestController
@RequestMapping("app/ins-line")
@Api(value = "巡检-线路", tags = "巡检-线路")
public class InsLineController {

    @Autowired
    private IInsLineService insLineService;

    @Autowired
    private IInsLinePointService insLinePointService;

    @Autowired
    private IInsPointLabelService insPointLabelService;

    /**
     * 获取巡检线路列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取巡检线路列表")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, insLineService.queryPage(params));
    }

    /**
     * 新增巡检线路
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增巡检线路")
    public R add(@RequestBody InsLineVO insLineVO) {
        if (insLineVO == null) {
            throw new RRException(RRExceptionEnum.PARAM_IS_EMPTY);
        }
        insLineService.add(insLineVO);
        return R.ok();
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除巡检线路(web)")
    public R delete(@RequestBody Integer[] lineIds) {
        insLineService.delete(Arrays.asList(lineIds));
        return R.ok();
    }

    @PostMapping(value = "/update")
    @ApiOperation("修改巡检线路(web)")
    public R update(@RequestBody InsLineVO insLineVO) {
        if (insLineVO == null) {
            throw new RRException(RRExceptionEnum.PARAM_IS_EMPTY);
        }
        insLineService.update(insLineVO);
        return R.ok();
    }

    @GetMapping(value = "/get/{id}")
    @ApiOperation("巡检线路-查看详情(web)")
    public R get(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, insLineService.detail(id));
    }


    @GetMapping(value = "/getPointList/{lineId}")
    @ApiOperation("巡检路线-查看路线(web)")
    public R getPointList(@PathVariable("lineId") Integer lineId) {
        List<String> result = insLinePointService.getInsPointListByLineId(lineId).stream()
                .map(InsPoint::getDeviceName)
                .collect(Collectors.toList());
        return R.ok().put(Constant.DATA, result);
    }

    @GetMapping(value = "/getTrack/{lineId}")
    @ApiOperation("巡检路线-查看轨迹(web)")
    public R getTrack(@PathVariable("lineId") Integer lineId) {
        InsTrackDTO result = insLineService.getInsTrackByLineId(lineId);
        return R.ok().put(Constant.DATA, result);
    }

    @GetMapping(value = "/getPointLabelList")
    @ApiOperation("巡检路线-查询检测点和标签集合(web/app)")
    public R getPointLabelList(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, insPointLabelService.getPointLabelList(params));
    }

}
